package com.itheima.leadnews.search.listener;

import com.heima.model.common.constant.CommonConstant;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ArticleSyncListener {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @KafkaListener(topics = CommonConstant.TOPIC_ARTICLE_SYNC)
    public void articleSync(ConsumerRecord<String,String> record) throws IOException {
        String id = record.key();
        String articleDoc = record.value();
        IndexRequest indexRequest = new IndexRequest("app_info_article");
        indexRequest.id(id);
        indexRequest.source(articleDoc, XContentType.JSON);
        restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
    }
}
